package com.jh.fcsm.util.security;

import com.jh.fcsm.constant.TokenConstants;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class RSAEncrypt {
    private static final Map<Integer, String> keyMap = new HashMap<>(); // 用于封装随机产生的公钥与私钥

    public static void main(String[] args) throws Exception {
        // 生成公钥和私钥
//        genKeyPair();
        // 加密字符串
        String message = "{\"data\":[{\"alwaysShow\":true,\"children\":[{\"component\":\"system/dict/index\",\"hidden\":false,\"meta\":{\"icon\":\"dict\",\"noCache\":false,\"title\":\"系统字典\"},\"name\":\"Dict\",\"path\":\"dict\"},{\"component\":\"system/menu/index\",\"hidden\":false,\"meta\":{\"icon\":\"tree-table\",\"noCache\":true,\"title\":\"功能节点管理\"},\"name\":\"Menu\",\"path\":\"menu\"},{\"component\":\"system/user/index\",\"hidden\":false,\"meta\":{\"icon\":\"people\",\"noCache\":true,\"title\":\"用户信息管理\"},\"name\":\"User\",\"path\":\"user\"},{\"component\":\"system/role/index\",\"hidden\":false,\"meta\":{\"icon\":\"peoples\",\"noCache\":true,\"title\":\"角色管理\"},\"name\":\"Role\",\"path\":\"role\"},{\"component\":\"system/logininfor/index\",\"hidden\":false,\"meta\":{\"icon\":\"logininfor\",\"noCache\":true,\"title\":\"访问日志\"},\"name\":\"Flog\",\"path\":\"flog\"},{\"component\":\"system/password/index\",\"hidden\":false,\"meta\":{\"icon\":\"system\",\"noCache\":true,\"title\":\"修改密码\"},\"name\":\"Password\",\"path\":\"password\"},{\"component\":\"system/fileList/index\",\"hidden\":false,\"meta\":{\"icon\":\"system\",\"noCache\":true,\"title\":\"文件查询\"},\"name\":\"File\",\"path\":\"file\"},{\"component\":\"system/operlog/index\",\"hidden\":false,\"meta\":{\"icon\":\"log\",\"noCache\":true,\"title\":\"操作日志\"},\"name\":\"Operlog\",\"path\":\"operlog\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"system\",\"noCache\":false,\"title\":\"系统管理\"},\"name\":\"System\",\"path\":\"/system\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"inf/code/index\",\"hidden\":false,\"meta\":{\"icon\":\"code\",\"noCache\":true,\"title\":\"接口配置\"},\"name\":\"Code\",\"path\":\"code\"},{\"component\":\"inf/app/index\",\"hidden\":false,\"meta\":{\"icon\":\"lock\",\"noCache\":true,\"title\":\"令牌秘钥配置\"},\"name\":\"App\",\"path\":\"app\"},{\"component\":\"inf/method/index\",\"hidden\":false,\"meta\":{\"icon\":\"component\",\"noCache\":true,\"title\":\"令牌权限配置\"},\"name\":\"Method\",\"path\":\"method\"},{\"component\":\"inf/log/index\",\"hidden\":false,\"meta\":{\"icon\":\"log\",\"noCache\":true,\"title\":\"接口日志记录\"},\"name\":\"Log\",\"path\":\"log\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"rate\",\"noCache\":false,\"title\":\"接口管理\"},\"name\":\"Inf\",\"path\":\"/inf\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"lscms/type/index\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"专业领域\"},\"name\":\"Type\",\"path\":\"type\"},{\"component\":\"basic/area/index\",\"hidden\":false,\"meta\":{\"icon\":\"404\",\"noCache\":false,\"title\":\"行政划分管理\"},\"name\":\"Base\",\"path\":\"base\"},{\"component\":\"lscms/field/index1\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"专业领域表格\"},\"name\":\"Index1\",\"path\":\"index1\"},{\"component\":\"lscms/manage/index\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"主管\"},\"name\":\"Manage\",\"path\":\"manage\"},{\"component\":\"lscms/field/index\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"专业领域\"},\"name\":\"Field\",\"path\":\"field\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"system\",\"noCache\":false,\"title\":\"基础信息管理\"},\"name\":\"Base\",\"path\":\"/base\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"tool/build/index\",\"hidden\":false,\"meta\":{\"icon\":\"build\",\"noCache\":true,\"title\":\"表单构建\"},\"name\":\"Build\",\"path\":\"build\"},{\"component\":\"tool/gen/index\",\"hidden\":false,\"meta\":{\"icon\":\"code\",\"noCache\":true,\"title\":\"代码生成\"},\"name\":\"Gen\",\"path\":\"gen\"},{\"component\":\"tool/swagger/index\",\"hidden\":false,\"meta\":{\"icon\":\"swagger\",\"noCache\":true,\"title\":\"系统接口\"},\"name\":\"Swagger\",\"path\":\"swagger\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"tool\",\"noCache\":false,\"title\":\"系统工具\"},\"name\":\"Tool\",\"path\":\"/tool\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"school/tutor/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"导师信息\"},\"name\":\"Tutor\",\"path\":\"tutor\"},{\"component\":\"school/student/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"学生信息\"},\"name\":\"Student\",\"path\":\"student\"},{\"component\":\"school/major/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"专业\"},\"name\":\"Major\",\"path\":\"major\"},{\"component\":\"school/dormitory/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"寝室\"},\"name\":\"Dormitory\",\"path\":\"dormitory\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"checkbox\",\"noCache\":false,\"title\":\"学校信息管理\"},\"name\":\"School\",\"path\":\"/school\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"phone/mi/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"小米手机信息\"},\"name\":\"Mi\",\"path\":\"mi\"},{\"component\":\"phone/warehouse/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"手机仓库信息\"},\"name\":\"Warehouse\",\"path\":\"warehouse\"},{\"component\":\"phone/huawei/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"华为手机信息\"},\"name\":\"Huawei\",\"path\":\"huawei\"},{\"component\":\"phone/iphone/index\",\"hidden\":false,\"meta\":{\"icon\":\"phone\",\"noCache\":false,\"title\":\"苹果手机信息\"},\"name\":\"Iphone\",\"path\":\"iphone\"},{\"component\":\"phone/oppo/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"OPPO手机信息\"},\"name\":\"Oppo\",\"path\":\"oppo\"},{\"component\":\"phone/store/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"手机商店信息\"},\"name\":\"Store\",\"path\":\"store\"},{\"component\":\"phone/recommend/index\",\"hidden\":false,\"meta\":{\"icon\":\"#\",\"noCache\":false,\"title\":\"手机推荐\"},\"name\":\"Recommend\",\"path\":\"recommend\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"phone\",\"noCache\":false,\"title\":\"手机管理\"},\"name\":\"Phone\",\"path\":\"/phone\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"goods/cup/index\",\"hidden\":false,\"meta\":{\"icon\":\"component\",\"noCache\":false,\"title\":\"杯子信息管理\"},\"name\":\"Cup\",\"path\":\"cup\"},{\"component\":\"goods/book/index\",\"hidden\":false,\"meta\":{\"icon\":\"guide\",\"noCache\":false,\"title\":\"书本信息管理\"},\"name\":\"Book\",\"path\":\"book\"},{\"component\":\"goods/pen/index\",\"hidden\":false,\"meta\":{\"icon\":\"drag\",\"noCache\":false,\"title\":\"钢笔信息管理\"},\"name\":\"Pen\",\"path\":\"pen\"},{\"component\":\"goods/mouse/index\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"鼠标信息管理\"},\"name\":\"Mouse\",\"path\":\"mouse\"},{\"component\":\"goods/keyboard/index\",\"hidden\":false,\"meta\":{\"icon\":\"dashboard\",\"noCache\":false,\"title\":\"键盘信息管理\"},\"name\":\"Keyboard\",\"path\":\"keyboard\"},{\"component\":\"goods/screen\",\"hidden\":false,\"meta\":{\"icon\":\"example\",\"noCache\":false,\"title\":\"显示屏管理\"},\"name\":\"Screen\",\"path\":\"screen\"},{\"component\":\"goods/audio/index\",\"hidden\":false,\"meta\":{\"icon\":\"druid\",\"noCache\":false,\"title\":\"音响信息管理\"},\"name\":\"Audio\",\"path\":\"audio\"},{\"component\":\"goods/egg/index\",\"hidden\":false,\"meta\":{\"icon\":\"international\",\"noCache\":false,\"title\":\"鸡蛋信息管理\"},\"name\":\"Egg\",\"path\":\"egg\"},{\"component\":\"goods/meat/index\",\"hidden\":false,\"meta\":{\"icon\":\"eye\",\"noCache\":false,\"title\":\"肉类信息管理\"},\"name\":\"Meat\",\"path\":\"meat\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"tool\",\"noCache\":false,\"title\":\"商品管理\"},\"name\":\"Goods\",\"path\":\"/goods\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"food/lu/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"鲁菜信息\"},\"name\":\"Lu\",\"path\":\"lu\"},{\"component\":\"food/dongbei/index\",\"hidden\":false,\"meta\":{\"icon\":\"date\",\"noCache\":false,\"title\":\"东北菜\"},\"name\":\"Dongbei\",\"path\":\"dongbei\"},{\"component\":\"food/chuan/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"川菜信息\"},\"name\":\"Chuan\",\"path\":\"chuan\"},{\"component\":\"food/yue/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"粤菜信息\"},\"name\":\"Yue\",\"path\":\"yue\"},{\"component\":\"food/zhe/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"浙菜信息\"},\"name\":\"Zhe\",\"path\":\"zhe\"},{\"component\":\"food/min/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"闽菜信息\"},\"name\":\"Min\",\"path\":\"min\"},{\"component\":\"food/xiang/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"湘菜信息\"},\"name\":\"Xiang\",\"path\":\"xiang\"},{\"component\":\"food/hui/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"徽菜信息\"},\"name\":\"Hui\",\"path\":\"hui\"},{\"component\":\"food/jiang/index\",\"hidden\":false,\"meta\":{\"icon\":\"star\",\"noCache\":false,\"title\":\"江苏菜信息\"},\"name\":\"Jiang\",\"path\":\"jiang\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"radio\",\"noCache\":false,\"title\":\"菜系管理\"},\"name\":\"Food\",\"path\":\"/food\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"hotel/cuisine/index\",\"hidden\":false,\"meta\":{\"icon\":\"button\",\"noCache\":false,\"title\":\"菜系信息\"},\"name\":\"Cuisine\",\"path\":\"cuisine\"},{\"component\":\"hotel/type/index\",\"hidden\":false,\"meta\":{\"icon\":\"chart\",\"noCache\":false,\"title\":\"菜品类型\"},\"name\":\"Type\",\"path\":\"type\"},{\"component\":\"hotel/dishes/index\",\"hidden\":false,\"meta\":{\"icon\":\"color\",\"noCache\":false,\"title\":\"菜品信息\"},\"name\":\"Dishes\",\"path\":\"dishes\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"edit\",\"noCache\":false,\"title\":\"吃什么\"},\"name\":\"Hotel\",\"path\":\"/hotel\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"team/tea/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"教师管理\"},\"name\":\"Tea\",\"path\":\"tea\"},{\"component\":\"team/grade/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"教师考核登记\"},\"name\":\"Grade\",\"path\":\"grade\"},{\"component\":\"team/staff/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"员工管理\"},\"name\":\"Staff\",\"path\":\"staff\"},{\"component\":\"team/scientist/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"科学家管理\"},\"name\":\"Scientist\",\"path\":\"scientist\"},{\"component\":\"team/doctor/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"医生管理\"},\"name\":\"Doctor\",\"path\":\"doctor\"},{\"component\":\"team/nurse/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"护士管理\"},\"name\":\"Nurse\",\"path\":\"nurse\"},{\"component\":\"team/progammer/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"程序员管理\"},\"name\":\"Progammer\",\"path\":\"progammer\"},{\"component\":\"team/lawyer/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"律师管理\"},\"name\":\"Lawyer\",\"path\":\"lawyer\"},{\"component\":\"team/farmer/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"农民管理\"},\"name\":\"Farmer\",\"path\":\"farmer\"},{\"component\":\"team/psychologist/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"心理学家管理\"},\"name\":\"Psychologist\",\"path\":\"psychologist\"},{\"component\":\"team/geographer/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"地理学家管理\"},\"name\":\"Geographer\",\"path\":\"geographer\"},{\"component\":\"team/biologist/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"生物学家管理\"},\"name\":\"Biologist\",\"path\":\"biologist\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"color\",\"noCache\":false,\"title\":\"人员管理\"},\"name\":\"Team\",\"path\":\"/team\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"scientist/physicist/index\",\"hidden\":false,\"meta\":{\"icon\":\"user\",\"noCache\":false,\"title\":\"物理学家管理\"},\"name\":\"Physicist\",\"path\":\"physicist\"},{\"component\":\"scientist/test\",\"hidden\":false,\"meta\":{\"noCache\":false,\"title\":\"测试数据\"},\"name\":\"Test\",\"path\":\"test\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"peoples\",\"noCache\":false,\"title\":\"科学家管理\"},\"name\":\"Scientist\",\"path\":\"/scientist\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"medical/register/index\",\"hidden\":false,\"meta\":{\"icon\":\"tool\",\"noCache\":false,\"title\":\"挂号\"},\"name\":\"Register\",\"path\":\"register\"},{\"component\":\"medical/diagnose/index\",\"hidden\":false,\"meta\":{\"icon\":\"tool\",\"noCache\":false,\"title\":\"诊断\"},\"name\":\"Medical\",\"path\":\"medical\"},{\"component\":\"medical/doctor/index\",\"hidden\":false,\"meta\":{\"icon\":\"peoples\",\"noCache\":false,\"title\":\"医生管理\"},\"name\":\"Doctor\",\"path\":\"doctor\"},{\"component\":\"medical/patient/index\",\"hidden\":false,\"meta\":{\"icon\":\"peoples\",\"noCache\":false,\"title\":\"患者管理\"},\"name\":\"Patient\",\"path\":\"patient\"},{\"component\":\"medical/department/index\",\"hidden\":false,\"meta\":{\"icon\":\"peoples\",\"noCache\":false,\"title\":\"部门管理\"},\"name\":\"Department\",\"path\":\"department\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"tool\",\"noCache\":false,\"title\":\"挂号诊断\"},\"name\":\"Medical\",\"path\":\"/medical\",\"redirect\":\"noRedirect\"},{\"alwaysShow\":true,\"children\":[{\"component\":\"office/word/index\",\"hidden\":false,\"meta\":{\"icon\":\"build\",\"noCache\":false,\"title\":\"word\"},\"name\":\"Word\",\"path\":\"word\"},{\"component\":\"ParentView\",\"hidden\":true,\"meta\":{\"icon\":\"404\",\"noCache\":false,\"title\":\"test\"},\"name\":\"Abc\",\"path\":\"abc\"}],\"component\":\"Layout\",\"hidden\":false,\"meta\":{\"icon\":\"form\",\"noCache\":false,\"title\":\"office\"},\"name\":\"Office\",\"path\":\"/office\",\"redirect\":\"noRedirect\"}],\"message\":\"成功\",\"status\":200}";
//        System.out.println("随机生成的公钥为:" + keyMap.get(0));
//        System.out.println("随机生成的私钥为:" + keyMap.get(1));
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0q3pc5amzV/opanqxDu7PzI6WTMfqFz00U2wycRmUXU+a/i2O0SAquBsSpDkE8aoG8bbNEUh1bISv6w7iX21Q6b8BtsNmiBKykS2pJTA+hXjsq+5EZTo5xQDSoYNObGp2WfzXFi902z3LdD2aseCm3mNb4f0mo/uADl4XqlSSrwIDAQAB";
//        String privateKey = TokenConstants.PRIVATE_KEY;
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOTWqATUVQaiZvBE6N21pcrUsOE48JfYhi77WN8xlQzcmygFfPSuQz81Qxua9JW2I30lTncNGvQNeDGlosOOaWa9/niFesyL+WcFqIDw/z0bRNLApDMLUHAFNUqhvMua011cQxozDjilXq5VAZdMsIn15hOrCXwWiN6gPlw7T/53AgMBAAECgYAggvrGDOCzm5kiY7ft3+PZKFSk+vD7sdCGlkEvHJ0/gYwtqHHzVVrKfSe2oVJEfucV3ZoDQgJlUBCWhSDVxcLliCwrIEc1gsvz9Adg6G4zJ/9nvjKKLYywRkF7P1jKi3FO159upk4P4M2UsUwEXXY7ICrrQXFm2EQEtCybVMDW6QJBAPMgZOWt1P1gqvJWEkbjmTcuT/3ULNg6hlJqu//OyaVbdKzD7jaTKsSOmUYhlwMIFee5XVFaUg1R/Mj3E2ijX/MCQQDw9JYZpX2sopytazTZXikRdzcwngd2OwHzMJMsgA+I3qXEIzIY4+mk8eoIZPIWZGN+KHBlDH/o955WRDDyrExtAkBM+j638dfz+X1miRX0V++U5xbwmRMxyyXoRl8L2V9C+bfxU3/Cy8nIvhWNfOkhTpgQ7EG9lklQalsNDh2ZDdE9AkEAoN0ZbIvQiDWT1i8xfEDhuBhYDywhnSwt1BbE2ruwI1dJYdWBd56I+KbM8Me723kv9yS2TK/4oSbXXY5vA0RGTQJAboZS7XmF9wmyIAGSABvR722jfbY3OjpDVBkaEFWdCxA77iLcmC98g0ZpC3V/avVi4brGhjms132PMv3q/UXNMw==";
//        String messageEn = publicKeyEncrypt(message, TokenConstants.PUBLIC_KEY);
//        System.out.println("加密后的字符串为:" + messageEn);

        String messageEn = "agZTUSyRASOg++hqwjdvZtSSLtJVZSw6tq/y+/iadQoc0Lnz7J7gIGsfPH+jDFMRM3G75eG9ksCbOuTwc6AaFUmEHQM+apR6LWJxhXy98qF9LLjDeyRFb+R2Kfkz7TRNjYTd2LHPLC5VxA0r9TWxjyKbGeK2kTFugR76XhUgVuFsRhhrd3DsW0sc0LmmTpT4bWjoV//7PzBJh/wSZL/RJM9xHiQmN0/SU6OzvqILU0z6Gl6OOpfg57iLjla/GFAZgXxDrutSVyrDy4qjTKekPQzIpYZ1rYVzyToglA/wp7v6SVwirO+qCQPJfWN4ctbMKP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Vq7pJgaYziPDJDGWRAB2WVhIY8bz9hQ0LOHOu90tkiKC2HVV+NBkDZPv7RooUY2c+fDzI1gFjgDOCcECFkdASVHoE8ovkda4QzauPlFMkHBZ7lbuIB1r+6UeMESZpMIq4K1jO1hQwsVGkDt83Z1rUBJJhIO3y6s3bfTAKo9ato6I8fPoMhhvroEqqNrhyfbuXGve6Ap3qRlHmsO59GFs=";
        String messageDe = privateKeyDecrypt(messageEn, privateKey);
        System.out.println("还原后的字符串为:" + messageDe);




    }

    /**
     * 随机生成密钥对
     */
    public static void genKeyPair() throws NoSuchAlgorithmException {
        // KeyPairGenerator类用于生成公钥和私钥对，基于RSA算法生成对象
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        // 初始化密钥对生成器，密钥大小为96-1024位
        keyPairGen.initialize(1024, new SecureRandom());
        // 生成一个密钥对，保存在keyPair中
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
        String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
        // 得到私钥字符串
        String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
        // 将公钥和私钥保存到Map
        keyMap.put(0, publicKeyString); // 0表示公钥
        keyMap.put(1, privateKeyString); // 1表示私钥
    }

    /**
     * 生成公钥私钥
     *
     * @return 公钥，私钥
     */
    public static Map<Integer, String> getSecret() throws NoSuchAlgorithmException {
        Map<Integer, String> map = new HashMap<>();
        // 生成公钥和私钥
        genKeyPair();
        // 将公钥和私钥保存到Map
        map.put(0, keyMap.get(0)); // 0表示公钥
        map.put(1, keyMap.get(1)); // 1表示私钥
        return map;
    }

    /**
     * RSA公钥加密
     *
     * @param str       加密字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String publicKeyEncrypt(String str, String publicKey) throws Exception {
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").
                generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        //当长度过长的时候，需要分割后加密 117个字节
        byte[] resultBytes = getMaxResultEncrypt(str, cipher);

        return Base64.encodeBase64String(resultBytes);
    }

    private static byte[] getMaxResultEncrypt(String str, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
        byte[] inputArray = str.getBytes();
        int inputLength = inputArray.length;
        // 最大加密字节数，超出最大字节数需要分组加密
        int MAX_ENCRYPT_BLOCK = 117;
        // 标识
        int offSet = 0;
        byte[] resultBytes = {};
        byte[] cache;
        while (inputLength - offSet > 0) {
            if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
                offSet += MAX_ENCRYPT_BLOCK;
            } else {
                cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
                offSet = inputLength;
            }
            resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
            System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
        }
        return resultBytes;
    }

    /**
     * RSA私钥解密
     *
     * @param str        加密字符串
     * @param privateKey 私钥
     * @return 铭文
     * @throws Exception 解密过程中的异常信息
     */
    public static String privateKeyDecrypt(String str, String privateKey) throws Exception {
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        //当长度过长的时候，需要分割后解密 128个字节
        return new String(getMaxResultDecrypt(str, cipher));
    }

    private static byte[] getMaxResultDecrypt(String str, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
        byte[] inputArray = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        int inputLength = inputArray.length;
        // 最大解密字节数，超出最大字节数需要分组加密
        int MAX_ENCRYPT_BLOCK = 128;
        // 标识
        int offSet = 0;
        byte[] resultBytes = {};
        byte[] cache;
        while (inputLength - offSet > 0) {
            if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
                offSet += MAX_ENCRYPT_BLOCK;
            } else {
                cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
                offSet = inputLength;
            }
            resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
            System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
        }
        return resultBytes;
    }
}
